package com.h3bpm.web.organization.service.impl;

import com.h3bpm.web.common.utils.SqlUtil;
import com.h3bpm.web.msg.util.StringUtils;
import com.h3bpm.web.organization.service.PushService;
import com.h3bpm.web.util.DateUtil;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author villwang
 * @date 2020/08/12
 **/
@Service
public class PushServiceImpl implements PushService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PushServiceImpl.class);
    /**
     *@描述 人事推送数据插入数据库
     *@参数  json 推送数据 flag 1为unit 2为人事
     *@返回值
     *@创建人 villwang
     *@创建时间
     *@修改人和其它信息
     */
    @Override
    public int intoDbo(String json, String flag) {
        LOGGER.info("插入更新数据——————————————"+json);
        int i =0;
        try {
            if (json!= null && !"".equals(json)) {
                String sql2="INSERT INTO dbo_push VALUES(0,'"+json+"',now(),'"+flag+"','0')";
                i = SqlUtil.doNoQuery(sql2);
                if(i>0){
                }else {
                    LOGGER.info("插入更新数据——————————————失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<HashMap<String, Object>> getDbo() {
        LOGGER.info("获取更新数据——————————————");
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        try {

//            String sql2="Select * from dbo_push where state=0 order by flag asc, time desc limit 1000";
            String sql2="Select * from dbo_push where state=0 order by flag asc, time asc limit 1000";
            DataTable table = SqlUtil.doQuery(sql2);
            List<DataRow> entityRows = table.getEntityRows();
            if(!entityRows.isEmpty()&&entityRows.size()>0) {
                for (DataRow userData : entityRows) {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    String json = userData.getString("json");
                    Integer  id = Integer.valueOf(userData.getString("id"));
                    String flag= userData.getString("flag");
                    hashMap.put("json",json);
                    hashMap.put("id",id);
                    hashMap.put("flag",flag);
                    list.add(hashMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public String updatDbo(int id) {
        LOGGER.debug("更新推送状态—————————————"+id);
        try {
            if (id!=0) {
                String sql2="UPDATE  dbo_push  SET STATE='1' WHERE ID="+ id ;
                SqlUtil.doNoQuery(sql2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String delDbo() {
        LOGGER.debug("删除历史数据开始—————————————");
        try {
          //获取当前时间的前一周的时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            //过去七天
            c.setTime(new Date());
            c.add(Calendar.DATE, - 7);
            Date d = c.getTime();
            String times = format.format(d);
            String sql2="delete from dbo_push  WHERE time<'"+ times+"'";
            SqlUtil.doNoQuery(sql2);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取未处理中间表用户
     * @return
     */
    @Override
    public List<HashMap<String, Object>> getUnprocessedDboUser() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        try {
            String sql2="Select * from dbo_user where status='0' limit 500";
            DataTable table = SqlUtil.doQuery(sql2);
            List<DataRow> entityRows = table.getEntityRows();
            for (DataRow userData : entityRows) {
                HashMap<String, Object> map = new HashMap<>();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("emp_id",userData.getString("emp_id"));
                hashMap.put("emp_idcode",userData.getString("emp_idcode"));
                hashMap.put("emp_number",userData.getString("emp_number"));
                hashMap.put("emp_name",userData.getString("emp_name"));
                hashMap.put("dept_id",userData.getString("dept_id"));
                hashMap.put("dept_idcode",userData.getString("dept_idcode"));
                hashMap.put("emp_idnumber",userData.getString("emp_idnumber"));
                hashMap.put("emp_mobilephone",userData.getString("emp_mobilephone"));
                hashMap.put("emp_projectleader",userData.getString("emp_projectleader"));
                hashMap.put("emp_projectpayee",userData.getString("emp_projectpayee"));
                hashMap.put("emp_judgeprojectleader",userData.getString("emp_judgeprojectleader"));
                hashMap.put("emp_judgeprojectpayee",userData.getString("emp_judgeprojectpayee"));
                hashMap.put("emp_incumbencystate",userData.getString("emp_incumbencystate"));
                hashMap.put("emp_entrydate",userData.getString("emp_entrydate"));
                hashMap.put("emp_quitdate",userData.getString("emp_quitdate"));
                hashMap.put("emp_projectleaderid",userData.getString("emp_projectleaderid"));
                hashMap.put("emp_projectpayeeid",userData.getString("emp_projectpayeeid"));
                hashMap.put("emp_sex",userData.getString("emp_sex"));
                hashMap.put("part_type",userData.getString("part_type"));
                hashMap.put("idtype_content",userData.getString("idtype_content"));
                hashMap.put("competentorg_content",userData.getString("competentorg_content"));
                hashMap.put("incumbency_content",userData.getString("incumbency_content"));
                hashMap.put("emp_numbereffectiveness",userData.getString("emp_numbereffectiveness"));
                hashMap.put("empisvalid_content",userData.getString("empisvalid_content"));
                hashMap.put("emp_datavalid",userData.getString("emp_datavalid"));
                hashMap.put("emp_companyemail",userData.getString("emp_companyemail"));
                hashMap.put("emp_personneltype",userData.getString("emp_personneltype"));
                hashMap.put("persontype_content",userData.getString("persontype_content"));
                map.put("flag", userData.getString("flag"));
                map.put("data", hashMap);
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<HashMap<String, Object>> getUnprocessedDboUnit() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        try {
            String sql2="Select * from dbo_unit where status='0' order by  flag,dept_grade   limit 500";
            DataTable table = SqlUtil.doQuery(sql2);
            List<DataRow> entityRows = table.getEntityRows();
            for (DataRow userData : entityRows) {
                HashMap<String, Object> map = new HashMap<>();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("dept_id",userData.getString("dept_id"));
                hashMap.put("dept_code",userData.getString("dept_code"));
                hashMap.put("dept_content",userData.getString("dept_content"));
                hashMap.put("dept_grade",userData.getString("dept_grade"));
                hashMap.put("dept_parentcode",userData.getString("dept_parentcode"));
                hashMap.put("dept_inchargeleader",userData.getString("dept_inchargeleader"));
                hashMap.put("dept_leader",userData.getString("dept_leader"));
                hashMap.put("dept_oaorgcode",userData.getString("dept_oaorgcode"));
                hashMap.put("dept_idcode",userData.getString("dept_idcode"));
                hashMap.put("dept_ordercode",userData.getString("dept_ordercode"));
                hashMap.put("dept_parentidcode",userData.getString("dept_parentidcode"));
                hashMap.put("dept_deleted",userData.getString("dept_deleted"));
                hashMap.put("dept_type",userData.getString("dept_type"));
                hashMap.put("dept_allcontent",userData.getString("dept_allcontent"));
                hashMap.put("dept_endmark",userData.getString("dept_endmark"));
                hashMap.put("dept_inchargeleaderid",userData.getString("dept_inchargeleaderid"));
                hashMap.put("dept_leaderid",userData.getString("dept_leaderid"));
                hashMap.put("dept_parentid",userData.getString("dept_parentid"));
                hashMap.put("dept_instituteleader",userData.getString("dept_instituteleader"));
                hashMap.put("dept_leader_approve",userData.getString("dept_leader_approve"));
                hashMap.put("dept_instituteleader_approve",userData.getString("dept_instituteleader_approve"));
                hashMap.put("dept_inchargeleader_approve",userData.getString("dept_inchargeleader_approve"));
                hashMap.put("dept_leaderid_approve",userData.getString("dept_leaderid_approve"));
                hashMap.put("dept_instituteleaderid_approve",userData.getString("dept_instituteleaderid_approve"));
                hashMap.put("dept_inchargeleaderid_approve",userData.getString("dept_inchargeleaderid_approve"));
                hashMap.put("dept_processtype",userData.getString("dept_processtype"));
                hashMap.put("dept_instituteleaderid",userData.getString("dept_instituteleaderid"));
                map.put("flag", userData.getString("flag"));
                map.put("data", hashMap);
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void updatDboUnitStatus(Object deptId) {
        try {
            if (deptId!= null && !"".equals(deptId)) {
                String sql2="UPDATE  dbo_unit  SET status='1' WHERE dept_idcode='"+ deptId+"'";
                SqlUtil.doNoQuery(sql2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updatDboUserStatus(Object empId) {
        try {
            if (empId!= null && !"".equals(empId)) {
                String sql2="UPDATE dbo_user SET status='1' WHERE emp_idcode='"+ empId+"'";
                SqlUtil.doNoQuery(sql2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
